package bloque;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class RegistroDocumentos extends Registro{
    
	private ArrayList<Integer> offDocs;
	private String palabra;
	
	public RegistroDocumentos(String xpalabra,ArrayList<Integer> array) {
		this.offDocs = array;
		this.palabra= xpalabra;
	}
	
	public RegistroDocumentos() {}
	
	
	 public void convertirBytesARegistro(byte[] xInput) throws IOException {

         ByteArrayInputStream bytesRegistro = new ByteArrayInputStream(xInput);
         DataInputStream input = new DataInputStream(bytesRegistro);

         offDocs = new ArrayList<Integer>();
         int tamanioListOffset= input.readInt();
         for(int i=0;i<tamanioListOffset;i++) {
        	 int puntero=input.readInt();
             offDocs.add(i,puntero);
         }
         
     	int tamanioPalabra=input.readInt();
		byte[] bytesTermino= new byte[tamanioPalabra];
		input.read(bytesTermino);
		palabra=new String(bytesTermino);

 }

 public byte[] convertirRegistroABytes() throws IOException {

         ByteArrayOutputStream bytesNodo = new ByteArrayOutputStream();
         DataOutputStream output = new DataOutputStream(bytesNodo);

         output.writeInt(offDocs.size());
         
         for(int i=0; i<offDocs.size();i++) {
                 output.writeInt(offDocs.get(i));             
         }
         
         output.writeInt(palabra.getBytes().length);
         output.write(palabra.getBytes());
         
         return bytesNodo.toByteArray();
 }
	
	public int getTamanioRegistro() {
		return offDocs.size()+Integer.SIZE/8;
	}


	public ArrayList<Integer> getOffDocs() {
		return offDocs;
	}


	public void setOffDocs(ArrayList<Integer> offDocs) {
		this.offDocs = offDocs;
	}


	public String getPalabra() {
		return palabra;
	}


	public void setPalabra(String palabra) {
		this.palabra = palabra;
	}


	
}
